package com.td.module.equipment.framework.core;

import com.td.module.equipment.framework.config.MqttClientVO;
import com.td.module.equipment.framework.config.MqttConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhanghe
 * @date 2023/4/15
 */
@Component
public class MqttClientLoader {

    @Value("${mqtt.enable:false}")
    private Boolean enable;

    @Resource
    private MqttConfig mqttConfig;

    @Resource
    private MqttClientBuilder mqttClientManager;

    @PostConstruct
    public void handleMqttEvent() {

        if (!enable) {
            return;
        }

        List<MqttClientVO> list = mqttConfig.getClientList();
        list.forEach(mqttClient -> {
            mqttClientManager.buildMqttClient(mqttClient.getClientId(), mqttClient.getUrl(), mqttClient.getUsername(), mqttClient.getPassword(), mqttClient.getDefaultTopic(), true);
        });

    }
}
